package com.web.controller.guest;

import com.common.apiresult.CommonResult;
import com.common.domain.dto.AddGuestRecordDTO;
import com.common.domain.dto.GuestRecordQueryDTO;
import com.service.serviceInterface.GuestService;
import com.service.utils.SecurityUtil;
import com.system.entity.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * @author liuxuhuang
 * @date 2023年04月16日 22:51
 * @desc 访客控制器   有效范围仅自己
 */
@RestController(value = "guestController")
@Api(value = "notice", tags = "访客控制器")
@RequestMapping(value = "/guest/guest")
@Slf4j
public class CommonController {
    @Resource
    private GuestService guestService;
    @Resource
    private SecurityUtil securityUtil;

    @GetMapping
    @ApiOperation(value = "获取访客信息，只能获取自己填的记录")
    public CommonResult get(GuestRecordQueryDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        return guestService.getData(dto, userId);
    }

    @PostMapping
    @ApiOperation(value = "添加访客信息")
    public CommonResult<Object> add(@RequestBody @Validated AddGuestRecordDTO dto) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return guestService.add(dto, loginUser.getUser().getId());
    }

    @PutMapping
    @ApiOperation(value = "修改访客信息")
    public CommonResult update(@RequestBody @Validated AddGuestRecordDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        return guestService.updateData(dto,userId);
    }

    @DeleteMapping
    @ApiOperation(value = "删除访客信息")
    public CommonResult delete(@RequestParam("id") @NotBlank(message = "参数不能为空") String id) {
        return guestService.delete(id);
    }
}
